package com.huawei.appgallery.foundation.ui.framework.widget.button;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.ViewGroup.LayoutParams;
import huawei.widget.HwButton;
import o.cjz;
import o.dky.o;
import o.Logger;
import o.epv;

public class FitWidthButton extends HwButton {
    private static String ˊ = "BaseDownloadButton";
    private int ʻ;
    private TextPaint ʼ;
    private CharSequence ʽ;
    private int ˋ;
    private int ˎ;
    private int ˏ;
    private int ॱ;
    private int ॱॱ;
    private boolean ᐝ;

    public FitWidthButton(Context context) {
        this(context, null);
    }

    public FitWidthButton(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public FitWidthButton(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.ˎ = epv.ˋ(getContext(), 64);
        this.ˋ = epv.ˋ(getContext(), 90);
        this.ˏ = epv.ˋ(getContext(), 8);
        this.ॱ = epv.ˋ(getContext(), 8);
        this.ᐝ = false;
        this.ʽ = null;
        this.ʼ = null;
        ˏ(context, attributeSet);
    }

    private void ˏ(Context context, AttributeSet attributeSet) {
        this.ʼ = new TextPaint();
        if (attributeSet != null) {
            TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, o.ʹॱ);
            try {
                this.ᐝ = obtainStyledAttributes.getBoolean(o.ʼʼ, false);
            } catch (Exception e) {
                Logger.ˋ(ˊ, "Exception:" + e.toString());
            } finally {
                obtainStyledAttributes.recycle();
            }
        }
        cjz.ˎ(this);
    }

    private synchronized void ˎ() {
        this.ʽ = getText();
        if (this.ʽ != null) {
            this.ॱॱ = ˊ();
            if (this.ॱॱ != this.ʻ) {
                if (this.ʽ != null && this.ʽ.length() > 0) {
                    ˏ(this.ॱॱ);
                }
            }
        }
        this.ʻ = this.ॱॱ;
    }

    private void ˏ(int i) {
        if (!this.ᐝ) {
            LayoutParams layoutParams = getLayoutParams();
            if (layoutParams != null) {
                int i2 = (this.ˏ + i) + this.ॱ;
                if (i2 >= this.ˎ && i2 <= this.ˋ) {
                    layoutParams.width = i2;
                } else if (i2 > this.ˋ) {
                    layoutParams.width = this.ˋ;
                } else {
                    layoutParams.width = this.ˎ;
                }
                setLayoutParams(layoutParams);
            }
        }
    }

    private int ˊ() {
        if (this.ʼ == null || this.ʽ == null) {
            return 0;
        }
        this.ʼ.set(getPaint());
        return (int) this.ʼ.measureText(this.ʽ.toString());
    }

    public void ॱ() {
        ˎ();
        invalidate();
    }
}
